<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Plant extends CI_Controller {
	// form validation message
	public $m_strPlant				= "ชื่อโรงควบคุม";
	public $m_strYear				= "ปี";
	public $m_strQuality			= "ค่าคุณภาพน้ำ";
	public $m_strRequired			= "กรุณากรอกข้อมูล %s";
	public $m_strMatches			= "ข้อมูลไม่ตรงกัน ";
	public $m_strInsertErrorMsg		= "ไม่สามารถเพิ่มโรงควบคุมได้ : พบชื่อโรงควบคุมซ้ำ";
	public $m_strInsertYearErrorMsg	= "ไม่สามารถเพิ่มปีได้ : พบปีซ้ำ";
	public $m_strUpdateErrorMsg		= "ไม่สามารถแก้ไขโรงควบคุมได้ : พบชื่อโรงควบคุมซ้ำ";
	public $m_strMonthArray   		= array( 
										"01" => "มกราคม", 
										"02" => "กุมภาพันธ์", 
										"03" => "มีนาคม", 
										"04" => "เมษายน", 
										"05" => "พฤษภาคม", 
										"06" => "มิถุนายน", 
										"07" => "กรกฎาคม", 
										"08" => "สิงหาคม", 
										"09" => "กันยายน", 
										"10" => "ตุลาคม", 
										"11" => "พฤศจิกายน", 
										"12" => "ธันวาคม", 
										"" => "error", 
										);

	public $m_dbQty_bod  	 		= "bod";
	public $m_dbQty_bod_out  		= "bod_out";
	public $m_dbQty_cod  	 		= "cod";
	public $m_dbQty_cod_out			= "cod_out";
	public $m_dbQty_do  			= "do";
	public $m_dbQty_do_out  		= "do_out";
	public $m_dbQty_ph  			= "ph";
	public $m_dbQty_ph_out  		= "ph_out";
	public $m_dbQty_tds  			= "tds";
	public $m_dbQty_tds_out  		= "tds_out";
	public $m_dbQty_ss  			= "ss";
	public $m_dbQty_ss_out  		= "ss_out";
	public $m_dbQty_tkn  			= "tkn";
	public $m_dbQty_tkn_out  		= "tkn_out";
	public $m_dbQty_grease  		= "grease";
	public $m_dbQty_grease_out  	= "grease_out";
	public $m_dbQty_cd  			= "cd";
	public $m_dbQty_cd_out  		= "cd_out";
	public $m_dbQty_cr  			= "cr";
	public $m_dbQty_cr_out  		= "cr_out";
	public $m_dbQty_ni 				= "ni";
	public $m_dbQty_ni_out  		= "ni_out";
	public $m_dbQty_cu 				= "cu";
	public $m_dbQty_cu_out  		= "cu_out";
	public $m_dbQty_zn  			= "zn";
	public $m_dbQty_zn_out  		= "zn_out";
	public $m_dbQty_pb  			= "pb";
	public $m_dbQty_pb_out  		= "pb_out";
	public $m_dbQty_salinity  		= "salinity";
	public $m_dbQty_salinity_out  	= "salinity_out";
	public $m_dbQty_turbidity  		= "turbidity";
	public $m_dbQty_turbidity_out 	= "turbidity_out";
	public $m_dbQty_color  			= "color";
	public $m_dbQty_color_out  		= "color_out";
	public $m_dbQty_tc  			= "tc";
	public $m_dbQty_tc_out  		= "tc_out";
	public $m_dbQty_fc  			= "fc";
	public $m_dbQty_fc_out  		= "fc_out";
	public $m_dbQty_coliphage  		= "coliphage";
	public $m_dbQty_coliphage_out 	= "coliphage_out";
	public $m_dbQty_helminth  		= "helminth";
	public $m_dbQty_helminth_out  	= "helminth_out";
	public $m_dbQty_residual  		= "residual";
	public $m_dbQty_residual_out  	= "residual_out";


	public function __construct()
	{
		session_start();
		parent::__construct();
		$this->load->helper('form');

		if ( !isset($_SESSION['username']) && !isset($_SESSION['user_level']))
		{
			// echo "session expired.<br/>";
			// echo uri_string();
			// echo "<br/>";
			// echo site_url();
			// echo "<br/>";
			// echo base_url();
			redirect('webadmin', 'refresh');
		}

	}

	public function index()
	{
		$this->load->model("plant_model");
		$rs = $this->plant_model->db_get_plantlist();

		$data["rs"] = $rs;
		$this->load->view("plant_list", $data);
	}

	public function plant_edit($a_pid)
	{
		$this->load->model("plant_model");
		$rs = $this->plant_model->db_get_yearlist_from_plant($a_pid);
		$plantname = $this->plant_model->db_get_plantname_from_pid($a_pid);

		$data["rs"] = $rs;
		$data["pid"] = $a_pid;
		$data["plantname"] = $plantname;
		$this->load->view("plant_edit", $data);
	}

	public function add_plant()
	{
		if ($_SESSION['user_level'] == "reader")
		{
			redirect("plant", "refresh");
		}

		$this->load->library('form_validation');

		$frm = $this->form_validation;
		$frm->set_rules('plant_name', $this->m_strPlant, 'required|trim|max_length[40]');
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);

		if ($frm->run() == FALSE)
		{
			$data["err_msg"] = NULL;
			$this->load->view('add_plant', $data);
		}
		else
		{
			//echo "Form success validation";
			$plant = $this->input->post("plant_name");

			$this->load->model("plant_model");
			$res = $this->plant_model->db_add_new_plant_by_id($plant);
			if ($res == TRUE)
			{
				redirect("plant", "refresh");
			}
			else
			{
				$data["err_msg"] = $this->m_strInsertErrorMsg;
				$this->load->view('add_plant', $data);
			}
		}
	}

	public function add_year($a_pid)
	{
		if ($_SESSION['user_level'] == "reader")
		{
			redirect("plant", "refresh");
		}

		$this->load->library('form_validation');

		$frm = $this->form_validation;
		$frm->set_rules('year_txt', $this->m_strYear, 'required|trim|max_length[20]');
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);

		if ($frm->run() == FALSE)
		{
			$data["err_msg"] = NULL;
			$data["pid"] = $a_pid;
			$this->load->view('add_year', $data);
		}
		else
		{
			//echo "Form success validation";
			$year = $this->input->post("year_txt");

			$this->load->model("plant_model");
			$res = $this->plant_model->db_add_new_year_by_id($a_pid, $year);
			if ($res == TRUE)
			{
				redirect("plant/plant_edit/".$a_pid, "refresh");
			}
			else
			{
				$data["err_msg"] = $this->m_strInsertYearErrorMsg;
				$data["pid"] = $a_pid;
				$this->load->view('add_year', $data);
			}
		}
	}
	
	public function delete_plant()
	{
		$chkbox = $this->input->post('mycheckbox');
		if ($chkbox)
		{
			$this->load->model("plant_model");
			foreach ($chkbox as $cb)
			{
				$this->plant_model->db_delete_plant_by_id($cb);
			}
		}
		redirect("plant", "refresh");
		
	}

	public function delete_year($a_pid)
	{
		//echo "delete year";
		$chkbox = $this->input->post('mycheckbox');
		if ($chkbox)
		{
			$this->load->model("plant_model");
			foreach ($chkbox as $cb)
			{
				$this->plant_model->db_delete_year_by_yid($cb);
			}
		}

		redirect("plant/plant_edit/".$a_pid, "refresh");
	}	

	// return NULL if empty, otherwise return value.
	public function getNullFromValue($a_value)
	{
		if(strlen($a_value) == 0)
			return NULL;
		return $a_value;
	}

	public function update_flow($a_pid, $a_yid, $a_date)
	{

		$this->load->library('form_validation');
		$frm = $this->form_validation;
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);
		$rule = 'trim|max_length[40]';
		$frm->set_rules("input[]",   $this->m_strQuality, $rule);

		$this->load->model("plant_model");
		$dailyList = $this->plant_model->db_get_date_from_yid($a_yid, $a_date);

		$data["str_year"] = $this->plant_model->db_get_yearname_from_yid($a_yid);
		$data["str_month"] = $this->m_strMonthArray[substr($a_date, 5, 2)]; //2000-01-01
		
		$data["pid"] = $a_pid;
		$data["yid"] = $a_yid;
		$data["date"] = $a_date;
		$data["rs"] = $dailyList;
		$data["err_msg"] = NULL;

		if ($frm->run() == FALSE)
		{
			$this->load->view('update_flow', $data);
		}
		else
		{
			//Form succeeded validation
			$frmInput = $this->input->post("input");
			$frmOutput = $this->input->post("output");

			// read all values from input
			$i = 0;
			$inputArray = array();
			foreach ($dailyList as $rec)
			{
				$tempArray = array(
				 	"date"			=> $rec->date,
				 	"yid"			=> $a_yid,
				 	"flow_in"		=> $this->getNullFromValue($frmInput[$i]),
				 	"flow_out"		=> $this->getNullFromValue($frmOutput[$i])
				);
				++$i;

				$inputArray[] = $tempArray;
			}
			// echo "<pre>";
			// print_r($inputArray);
			// echo "</pre>";
			//exit();

			$this->load->model("plant_model");
			$res = $this->plant_model->db_update_flow($inputArray);

			if ($res)
			{
				redirect("plant/plant_edit/".$a_pid, "refresh");
			}
			else
			{
			 	$data["err_msg"] = "error";
			 	$this->load->view('update_flow', $data);
			}
			
		}
	}

	public function update_quality($a_pid, $a_yid, $a_date)
	{

		$this->load->library('form_validation');

		$frm = $this->form_validation;
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);

		$rule = 'trim|max_length[40]';
		$frm->set_rules($this->m_dbQty_bod,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_bod_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cod,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cod_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_do,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_do_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_ph,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_ph_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_tds,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_tds_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_ss,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_ss_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_tkn,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_tkn_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_grease,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_grease_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cd,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cd_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cr,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cr_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_ni,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_ni_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cu,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_cu_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_zn,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_zn_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_pb,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_pb_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_salinity,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_salinity_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_turbidity,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_turbidity_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_color,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_color_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_tc,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_tc_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_fc,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_fc_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_coliphage,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_coliphage_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_helminth,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_helminth_out,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_residual,   $this->m_strQuality, $rule);
		$frm->set_rules($this->m_dbQty_residual_out,   $this->m_strQuality, $rule);

		$this->load->model("plant_model");
		$row = $this->plant_model->db_get_row_from_yid($a_yid, $a_date);
		$data["str_year"] = $this->plant_model->db_get_yearname_from_yid($a_yid);
		$data["str_month"] = $this->m_strMonthArray[substr($a_date, 5, 2)]; //2000-01-01
		$data["pid"] = $a_pid;
		$data["date"] = $a_date;
		$data["row"] = $row;
		$data["err_msg"] = NULL;

		if ($frm->run() == FALSE)
		{
			//echo "failed";
			$this->load->view('update_quality', $data);
		}
		else
		{
			//Form succeeded validation
			$inputArray = array(
				"yid"			=> $a_yid,
				"date"			=> $a_date,
				$this->m_dbQty_bod    => $this->getNullFromValue($this->input->post($this->m_dbQty_bod)),
				$this->m_dbQty_bod_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_bod_out)),
				$this->m_dbQty_cod    => $this->getNullFromValue($this->input->post($this->m_dbQty_cod)),
				$this->m_dbQty_cod_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_cod_out)),
				$this->m_dbQty_do    => $this->getNullFromValue($this->input->post($this->m_dbQty_do)),
				$this->m_dbQty_do_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_do_out)),
				$this->m_dbQty_ph    => $this->getNullFromValue($this->input->post($this->m_dbQty_ph)),
				$this->m_dbQty_ph_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_ph_out)),
				$this->m_dbQty_tds    => $this->getNullFromValue($this->input->post($this->m_dbQty_tds)),
				$this->m_dbQty_tds_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_tds_out)),
				$this->m_dbQty_ss    => $this->getNullFromValue($this->input->post($this->m_dbQty_ss)),
				$this->m_dbQty_ss_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_ss_out)),
				$this->m_dbQty_tkn    => $this->getNullFromValue($this->input->post($this->m_dbQty_tkn)),
				$this->m_dbQty_tkn_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_tkn_out)),
				$this->m_dbQty_grease    => $this->getNullFromValue($this->input->post($this->m_dbQty_grease)),
				$this->m_dbQty_grease_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_grease_out)),
				$this->m_dbQty_cd    => $this->getNullFromValue($this->input->post($this->m_dbQty_cd)),
				$this->m_dbQty_cd_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_cd_out)),
				$this->m_dbQty_cr    => $this->getNullFromValue($this->input->post($this->m_dbQty_cr)),
				$this->m_dbQty_cr_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_cr_out)),
				$this->m_dbQty_ni    => $this->getNullFromValue($this->input->post($this->m_dbQty_ni)),
				$this->m_dbQty_ni_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_ni_out)),
				$this->m_dbQty_cu    => $this->getNullFromValue($this->input->post($this->m_dbQty_cu)),
				$this->m_dbQty_cu_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_cu_out)),
				$this->m_dbQty_zn    => $this->getNullFromValue($this->input->post($this->m_dbQty_zn)),
				$this->m_dbQty_zn_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_zn_out)),
				$this->m_dbQty_pb    => $this->getNullFromValue($this->input->post($this->m_dbQty_pb)),
				$this->m_dbQty_pb_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_pb_out)),
				$this->m_dbQty_salinity    => $this->getNullFromValue($this->input->post($this->m_dbQty_salinity)),
				$this->m_dbQty_salinity_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_salinity_out)),
				$this->m_dbQty_turbidity    => $this->getNullFromValue($this->input->post($this->m_dbQty_turbidity)),
				$this->m_dbQty_turbidity_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_turbidity_out)),
				$this->m_dbQty_color    => $this->getNullFromValue($this->input->post($this->m_dbQty_color)),
				$this->m_dbQty_color_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_color_out)),
				$this->m_dbQty_tc    => $this->getNullFromValue($this->input->post($this->m_dbQty_tc)),
				$this->m_dbQty_tc_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_tc_out)),
				$this->m_dbQty_fc    => $this->getNullFromValue($this->input->post($this->m_dbQty_fc)),
				$this->m_dbQty_fc_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_fc_out)),
				$this->m_dbQty_coliphage    => $this->getNullFromValue($this->input->post($this->m_dbQty_coliphage)),
				$this->m_dbQty_coliphage_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_coliphage_out)),
				$this->m_dbQty_helminth    => $this->getNullFromValue($this->input->post($this->m_dbQty_helminth)),
				$this->m_dbQty_helminth_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_helminth_out)),
				$this->m_dbQty_residual    => $this->getNullFromValue($this->input->post($this->m_dbQty_residual)),
				$this->m_dbQty_residual_out    => $this->getNullFromValue($this->input->post($this->m_dbQty_residual_out))
			);

			$this->load->model("plant_model");
			$res = $this->plant_model->db_update_quality($inputArray);

			if ($res)
			{
				redirect("plant/plant_edit/".$a_pid, "refresh");
			}
			else
			{
				$data["err_msg"] = $this->m_strUpdateErrorMsg." = ". $plantName;
				$this->load->view('update_plant', $data);
			}
		}
	}

	public function rename_plant($a_pid)
	{
		if ($_SESSION['user_level'] == "reader")
		{
			redirect("plant", "refresh");
		}

		$this->load->library('form_validation');

		$frm = $this->form_validation;
		$frm->set_rules('plant_name', $this->m_strPlant, 'required|trim|max_length[40]');
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);

		$this->load->model("plant_model");
		$row = $this->plant_model->db_get_row_from_pid($a_pid);

		//echo "<pre>";
		//print_r($row);
		//echo "</pre>";

		$data["row"] = $row;
		$data["err_msg"] = NULL;

		if ($frm->run() == FALSE)
		{
			//echo "failed";
			$this->load->view('rename_plant', $data);
		}
		else
		{
			//Form succeeded validation
			$plantName = $this->input->post("plant_name");

			$this->load->model("plant_model");
			$res = $this->plant_model->db_rename_plant($a_pid, $plantName);

			if ($res)
			{
				redirect("plant", "refresh");
			}
			else
			{
				$data["err_msg"] = $this->m_strUpdateErrorMsg." = ". $plantName;
				$this->load->view('rename_plant', $data);
			}
		}
	}	
}
